package cn.com.manager.utility;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

import cn.com.manager.pojo.User;

@Service
public class PasswordHelper {
	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

	public void encryptPassword(User user) {
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		String newPassword = new SimpleHash(ApplicationConstants.algorithmName, user.getPassword(),
				ByteSource.Util.bytes(user.getCredentialsSalt()), ApplicationConstants.hashIterations).toHex();
		user.setPassword(newPassword);
	}
}
